add_rules("mode.debug", "mode.release", "mode.releasedbg")

-- 添加自动生成compile_commands.json的规则，为clangd提供编译数据库
add_rules("plugin.compile_commands.autoupdate", {outputdir = "."})

-- 添加第三方依赖库
add_requires("fmt", "spdlog")

target("mygpu")
    set_kind("binary")
    add_files("src/cuda_operations.cu")
    add_files("src/main.cpp")
    add_packages("fmt", "spdlog")
    set_toolchains("zig")
    set_plat("windows") -- 或者其他你的目标平台，如 "linux", "macosx"
    set_arch("x64")     -- 根据你的目标架构设置
    -- 设置使用动态链接的运行时库
    set_runtimes("MD")
    
    add_includedirs("C:/Users/qingz/AppData/Local/microsoft/winget/packages/zig.zig_Microsoft.Winget.Source_8wekyb3d8bbwe/zig-x86_64-windows-0.15.1/lib/include")
    
    
    -- 在Windows平台下为cl编译器和nvcc设置UTF-8编码选项
    if is_plat("windows") then
        -- 直接传递给cl编译器的UTF-8选项
        add_cxxflags("/utf-8")
        -- 通过-Xcompiler参数传递给nvcc的UTF-8选项
        add_cuflags("-Xcompiler=\"/utf-8\"")
        -- 传递给nvcc的宏定义 FMT_UNICODE=0
        add_cuflags("-DFMT_UNICODE=0")
        
        -- 为nvcc添加抑制警告的编译标志
        add_cuflags("-diag-suppress=27")   -- 抑制fmt中的字符值超出范围警告
        add_cuflags("-diag-suppress=128")  -- 抑制fmt中的"loop is not reachable"警告
        add_cuflags("-diag-suppress=2417") -- 抑制fmt中的constexpr构造函数警告
        add_cuflags("-diag-suppress=1388") -- 抑制spdlog中的DLL接口规范不一致警告
        add_cuflags("-diag-suppress=1394") -- 抑制spdlog中的DLL接口字段警告
    end

    -- generate SASS code for SM architecture of current host
    add_cugencodes("native")

    -- generate PTX code for the virtual architecture to guarantee compatibility
    -- add_cugencodes("compute_75")
    
    -- 构建后将依赖的DLL文件拷贝到二进制输出目录
    after_build(function (target)
        if is_plat("windows") then
            -- 拷贝fmt和spdlog的DLL文件到二进制目录
            local dll_paths = {}
            for _, pkg in ipairs({"fmt", "spdlog"}) do
                local pkginfo = target:pkg(pkg)
                if pkginfo then
                    local bindir = path.join(pkginfo.installdir, "bin")
                    if os.isdir(bindir) then
                        for _, dllfile in ipairs(os.files(path.join(bindir, "*.dll"))) do
                            table.insert(dll_paths, dllfile)
                        end
                    end
                end
            end
            
            if #dll_paths > 0 then
                local targetdir = target:targetdir()
                print("正在拷贝依赖的DLL文件到: " .. targetdir)
                for _, dllpath in ipairs(dll_paths) do
                    os.cp(dllpath, targetdir)
                    print("  已拷贝: " .. path.filename(dllpath))
                end
            end
        end
    end)

    -- -- generate SASS code for each SM architecture
    -- add_cugencodes("sm_35", "sm_37", "sm_50", "sm_52", "sm_60", "sm_61", "sm_70", "sm_75")

    -- -- generate PTX code from the highest SM architecture to guarantee forward-compatibility
    -- add_cugencodes("compute_75")

--
-- If you want to known more usage about xmake, please see https://xmake.io
--
-- ## FAQ
--
-- You can enter the project directory firstly before building project.
--
--   $ cd projectdir
--
-- 1. How to build project?
--
--   $ xmake
--
-- 2. How to configure project?
--
--   $ xmake f -p [macosx|linux|iphoneos ..] -a [x86_64|i386|arm64 ..] -m [debug|release]
--
-- 3. Where is the build output directory?
--
--   The default output directory is `./build` and you can configure the output directory.
--
--   $ xmake f -o outputdir
--   $ xmake
--
-- 4. How to run and debug target after building project?
--
--   $ xmake run [targetname]
--   $ xmake run -d [targetname]
--
-- 5. How to install target to the system directory or other output directory?
--
--   $ xmake install
--   $ xmake install -o installdir
--
-- 6. Add some frequently-used compilation flags in xmake.lua
--
-- @code
--    -- add debug and release modes
--    add_rules("mode.debug", "mode.release")
--
--    -- add macro definition
--    add_defines("NDEBUG", "_GNU_SOURCE=1")
--
--    -- set warning all as error
--    set_warnings("all", "error")
--
--    -- set language: c99, c++11
--    set_languages("c99", "c++11")
--
--    -- set optimization: none, faster, fastest, smallest
--    set_optimize("fastest")
--
--    -- add include search directories
--    add_includedirs("/usr/include", "/usr/local/include")
--
--    -- add link libraries and search directories
--    add_links("tbox")
--    add_linkdirs("/usr/local/lib", "/usr/lib")
--
--    -- add system link libraries
--    add_syslinks("z", "pthread")
--
--    -- add compilation and link flags
--    add_cxflags("-stdnolib", "-fno-strict-aliasing")
--    add_ldflags("-L/usr/local/lib", "-lpthread", {force = true})
--
-- @endcode
--

